package com.prothink.threadadmindemo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Locale;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


public class Liuyihao {
    private static Lock lk = new ReentrantLock();
    public static void main(String[] args) {
//        int [] arr={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
//        int n=10;
//        System.out.println(binarySearch(arr, n));

//
        new Thread(new myThread()).start();
        new Thread(new myThread()).start();
    }
    public static class myThread implements Runnable {

        @Override
        public void run() {

            for (int i = 0; i <= 100; i++) {
                lk.lock();
                try {

                if (i % 2 == 0){
                    System.out.println(Thread.currentThread().getName()+"==="+i+"是偶数");
                }
                if (i % 2 != 0){
                    System.out.println(Thread.currentThread().getName()+"==="+i+"是奇数");
                }
                }finally {
                    lk.unlock();
                }

            }

        }
    }

//    二分查找
        public static int binarySearch(int[] arr, int target) {
//            数组必须是有序的
            int i=0,j=arr.length-1;//i 和 j 分别为左右边界 数组的索引号
            while (i<=j){
                int index=(i+j)/2;
                if (arr[index]==target){
                    return index;//找到了
                }else if (arr[index]>target){
                    j=index-1; //中间数》要找的数。要找到数应该在左边，移动的指针到中间位置偏左一个
                }else {
                    i=index+1;
                }
            }
            return -1;//没找到
        }

}
